package com.example.braden.answergame.common.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;

import com.example.braden.answergame.R;

/**
 * Created by braden on 2018/3/23.
 */

public class LoadingDialog extends Dialog {
    private static Activity mContext;
    private boolean isShow;
    @BindView(R.id.tv_text)
    TextView tvText;

    public LoadingDialog(@NonNull Context context) {
        super(context);
        mContext = (Activity) context;
        init();
    }

    private LoadingDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mContext = (Activity) context;
        init();
    }

    public static class SingletonHolder {
        public static LoadingDialog dialog = new LoadingDialog(mContext, R.style.dialog);
    }

    public static LoadingDialog getInstance(Activity context) {
        if (mContext == null) {
            mContext = context;
        }
        return SingletonHolder.dialog;
    }

    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.loading_dialog, null);
        setContentView(view);
        ButterKnife.bind(this);
        this.getWindow().setWindowAnimations(R.style.dialog);
        this.setCanceledOnTouchOutside(false);
    }

    public void showDialog(String text) {
        if (mContext.isFinishing()) return;
        tvText.setText(text);
        if (!isShowing()) {
            show();
        }
    }

    public void dismissDialog() {
        if (!mContext.isFinishing() && isShowing()) {
            dismiss();
        }
    }

    public void setIsCancle(boolean isCancle) {
        this.setCanceledOnTouchOutside(isCancle);
        this.setCancelable(isCancle);
    }
}
